<?php
/**
 * @Author: kaihui.wang
 * @Contact  hpuwang@gmail.com
 * @Version: 1.0.0
 * @Date: 2023/6/30 15:41
 */
namespace Eduxplus\CoreBundle\Controller;

use Eduxplus\CoreBundle\Lib\Base\BaseAdminController;
use Eduxplus\CoreBundle\Service\DraftService;
use Symfony\Component\HttpFoundation\Request;
use Eduxplus\CoreBundle\Lib\Grid\Grid;
class DraftController extends BaseAdminController
{

    public function listAction($type, Request $request,Grid $grid,DraftService $draftService){
        $pageSize = 20;
        $grid->setListService($draftService, "getList", ["uid"=>$this->getUid(), "type"=>$type]);

        $grid->text("版本号")->field("version");
        $grid->text("时间")->field("updatedAt");
        $grid->deleteAction("admin_api_draft_delete");
        $grid->setTableAction('admin_draft_list', function ($obj) use($draftService){
            $id = $draftService->getPro($obj, "id");
            $type = $draftService->getPro($obj, "type");
            $url = "#";
            if($type == '200'){
                $url = $this->generateUrl('admin_teach_course_add')."?draftId=".$id;
            } else if($type == '400'){
                $url = $this->generateUrl('admin_cms_help_add')."?draftId=".$id;
            }
            else if($type == '100'){
                $url = $this->generateUrl('admin_cms_news_add')."?draftId=".$id;
            }
            else if($type == '300'){
                $url = $this->generateUrl('admin_cms_tutorial_add')."?draftId=".$id;
            }
            else if($type == '500'){
                $url = $this->generateUrl('admin_cms_page_add')."?draftId=".$id;
            }
            else if($type == '600'){
                $url = $this->generateUrl('admin_mall_goods_add')."?draftId=".$id;
            }
            else if($type == '601'){
                $url = $this->generateUrl('admin_mall_group_goods_add')."?draftId=".$id;
            }
            else if($type == '602'){
                $url = $this->generateUrl('admin_mall_coupon_add')."?draftId=".$id;
            }
            else if($type == '700'){
                $url = $this->generateUrl('admin_teach_product_add')."?draftId=".$id;
            }
            else if($type == '800'){
                $url = $this->generateUrl('admin_teach_agreement_add')."?draftId=".$id;
            }
            else if($type == '801'){
                $url = $this->generateUrl('admin_jw_school_add')."?draftId=".$id;
            }else if($type == '802'){
                $url = $this->generateUrl('admin_jw_teacher_add')."?draftId=".$id;
            }
            $str = '<a href="javascript:;" data-url=' . $url . ' title="编辑"  class="btn btn-info btn-xs newTab"><i class="mdi mdi-file-document-edit"></i>编辑</a>';
            return  $str;
        });
        //批量删除
        $grid->setBathDelete("admin_api_draft_bathdelete");
        return $this->content()->renderList($grid->create($request, $pageSize));
    }

    public function deleteDoAction($id, DraftService $draftService)
    {
        $draftService->del($id);
        return $this->responseMsgReload("删除成功!");
    }

    
    public function bathDeleteDoAction(Request $request, DraftService $draftService)
    {
        $ids = $request->get("ids");
        if($ids){
            foreach ($ids as $id){
                $draftService->del($id);
            }
        }

        return $this->responseMsgReload("删除成功!");
    }

    public function getLatestByTypeAction($type, DraftService $draftService){
        $info = $draftService->getLatestByType($type);
        return $this->responseSuccess($info);
    }

    public function getByIdAction(Request $request, DraftService $draftService){
        $id = $request->get("id");
        $info = $draftService->getById($id);
        return $this->responseSuccess($info);
    }

    public function autoSaveAction(Request $request, DraftService $draftService){
        $uid = $this->getUid();
        $type = $request->get("type");
        $version = $request->get("version");
        $content = $request->get("content");
        if(!$type){
            return $this->responseError("type 不能为空");
        }
        if(!$content){
            return $this->responseError("content 不能为空");
        }
        if($version){
            $info = $draftService->getLatestByVersion($version, $uid);
            if($info){
                $draftService->updateByVersion($version, $content, $uid);
            }else{
                $version = $draftService->add($uid, $type, $content);
            }
        }else{
            $version = $draftService->add($uid, $type, $content);
        }
        return $this->responseSuccess(["version"=>$version]);
    }

}
